<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { propApi } from '@/api/controller';
  import { ToastError, ToastSuccess, Toastloading, allNumber } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
    goodsClass:[] as Array<any>
  })
  interface formQuert {
    sku_id:number
    goods_id:number
    time_limit:number
    price:number
  }
  let form = reactive({} as formQuert)
  const formRules = {
    time_limit: [{ required: true, message: "请输入商品天数", trigger: "blur" }],
    price: [{ required: true, message: "请输入商品价格(钻石)", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      let submitSet = false
      const { goods_id, sku_id } = form
      const loading = Toastloading()
      if(!sku_id) {
        await propApi.createSku(form).then(()=>{
          submitSet = true
          ToastSuccess('新增商品价格成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await propApi.updateSku(sku_id,form).then(()=>{
          submitSet = true
          ToastSuccess('编辑商品价格成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      goods_id=0, time_limit='', price=0, goods_name, sku_id
    } = query
    Object.assign(form,{
      goods_id,
      time_limit,
      price,
      sku_id
    })
    action.title = `${goods_id?'编辑商品：':'新增商品'}${goods_name}价格`
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="商品天数" prop="time_limit">
          <el-input v-model="form.time_limit" placeholder="请输入商品天数"  />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="商品价格(钻石)" prop="price">
          <el-input v-model="form.price" @input="allNumber(form,'price')" placeholder="请输入商品价格(钻石)"  />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>